﻿using Avalonia;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FantomNext.Helpers
{
    public class RatioHelper
    {
        /// <summary>
        /// 在指定大小的外框中计算出一个保持指定比例的内矩形
        /// </summary>
        /// <param name="outerSize">外框的大小</param>
        /// <param name="innerRatio">内矩形的宽高比（宽 / 高）</param>
        /// <returns>计算得到的内矩形</returns>
        public static Size CalculateInnerRectangle(Size outerSize, double innerRatio)
        {
            double outerWidth = outerSize.Width;
            double outerHeight = outerSize.Height;

            // 计算外框的宽高比
            double outerRatio = outerWidth / outerHeight;

            double innerWidth;
            double innerHeight;

            if (outerRatio > innerRatio)
            {
                // 外框更宽，以内框高度为基准进行缩放
                innerHeight = outerHeight;
                innerWidth = innerHeight * innerRatio;
            }
            else
            {
                // 外框更高，以内框宽度为基准进行缩放
                innerWidth = outerWidth;
                innerHeight = innerWidth / innerRatio;
            }

            return new Size(innerWidth, innerHeight);
        }
    }
}
